package com.bw.goods.mvvm.viewmodel;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.EncryptUtils;
import com.bw.goods.R;
import com.bw.goods.mvvm.adapter.HomeGoodsAdapter;
import com.bw.goods.mvvm.model.HomeModel;
import com.bw.goods.mvvm.model.entity.HomeBannerEntity;
import com.bw.goods.mvvm.model.entity.HomeGoodsEntity;
import com.bw.goods.mvvm.model.entity.HomeMenuEntity;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.filed.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.shopping.mvvm.view.CommodityActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Observable;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author KK
 * @description:
 * @date :2021/10/14 16:05
 */
public class HomeViewModel extends BaseViewModel<HomeModel> {

    public String[]homeBanneres={"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=259364631,2307942273&fm=26&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3765821666,4235344830&fm=26&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3336810809,2729597475&fm=26&gp=0.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503471047&di=679d7a6c499f59d1b0dcd56b62a9aa6c&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.90sheji.com%2Fdianpu_cover%2F11%2F14%2F64%2F55%2F94ibannercsn_1200.jpg"};
    public int[]menuIcons = {R.drawable.ic_zhibo,R.drawable.ic_diannao,
            R.drawable.ic_shouji,R.drawable.ic_pad,R.drawable.ic_jiaju,
            R.drawable.ic_jiadian,R.drawable.ic_yundong,R.drawable.ic_xiuxian};
    public String[] menuNames = {"直播","电脑","手机","Pad",
            "家居","家电","秒杀","休闲"};
    public ObservableField<List<HomeBannerEntity>> homeBannerEntities = new ObservableField<>();
    public List<HomeBannerEntity> homeBannerlist = new ArrayList<>();
    public List<HomeMenuEntity> menus = new ArrayList<>();
    public List<String> hotImages = new ArrayList<>();
    public List<String> textBanners = new ArrayList<>();
    public List<HomeBannerEntity>topEntities = new ArrayList<>();
    public BaseQuickAdapter adapter = new HomeGoodsAdapter();
    public int orientation = LinearLayoutManager.VERTICAL;

    public GalleryPageTransformer transformer = new GalleryPageTransformer();
    //画廊
    public static class GalleryPageTransformer implements ViewPager.PageTransformer {

        private final static float MAX = 1f;//最大显示倍数
        private final static float MIN = 0.75f;//最小显示倍数

        @Override
        public void transformPage( View page,
                                  float position) {
            if (position<0){
                float scal = 1+position/3;//当前缩放倍数
                if (scal<MIN)
                    scal = MIN;
                page.setScaleY(scal);
            }else if (position>=0&&position<1){
                if (position == 0){
                    page.setScaleY(MAX);
                }else {
                    float scal = MIN + (1-position)/3;
                    if (scal > MAX)
                        scal = MAX;
                    page.setScaleY(scal);
                }
            }else if (position >= 1){
                page.setScaleY(MIN);
            }
        }
    }

    //创建顶部banner数据
    private void createBanner(){
        for (String str : homeBanneres) {
            homeBannerlist.add(new HomeBannerEntity(str,"https://www.baidu.com/"));
        }
        homeBannerEntities.set(homeBannerlist);
    }
    //创建菜单数据
    private void createMenu(){
        for (int i = 0; i < menuNames.length; i++) {
            menus.add(new HomeMenuEntity(menuIcons[i],menuNames[i],""));
        }
    }
    //创建文字banner数据
    private void createTextBanner(){
        textBanners.add("夏日炎炎，有你真甜");
        textBanners.add("新用户，领1000代金券");
        textBanners.add("10周年店庆，全场8折");
        textBanners.add("国庆特价大酬宾");

    }
    //创建特价商品数据
    private void createHotGoods(){
        hotImages.add("https://img14.360buyimg.com/n0/jfs/t3157/231/5756125504/98807/97ab361d/588084a1N06efb01d.jpg");
        hotImages.add("https://img10.360buyimg.com/n7/jfs/t5905/106/1120548052/61075/6eafa3a5/592f8f7bN763e3d30.jpg");
        hotImages.add("https://img10.360buyimg.com/n7/jfs/t5584/99/6135095454/371625/423b9ba5/59681d91N915995a7.jpg");
        hotImages.add("https://img11.360buyimg.com/n7/jfs/t4447/301/1238553109/193354/13c7e995/58db19a7N25101fe4.jpg");
        hotImages.add("https://img14.360buyimg.com/n1/s190x190_jfs/t7525/189/155179632/395056/e200017f/598fb8a4N7800dee6.jpg");
        hotImages.add("https://img10.360buyimg.com/n7/jfs/t5584/99/6135095454/371625/423b9ba5/59681d91N915995a7.jpg");
        hotImages.add("https://img10.360buyimg.com/n7/jfs/t5584/99/6135095454/371625/423b9ba5/59681d91N915995a7.jpg");
    }
    //创建首页vp数据
    private void createVp(){
        topEntities.add(new HomeBannerEntity("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2069038579,1558413647&fm=26&gp=0.jpg","https://www.baidu.com/"));
        topEntities.add(new HomeBannerEntity("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1975162372,3010774958&fm=26&gp=0.jpg","https://www.baidu.com/"));
        topEntities.add(new HomeBannerEntity("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1034894772,889898861&fm=26&gp=0.jpg","https://www.baidu.com/"));
        topEntities.add(new HomeBannerEntity("http://img.zcool.cn/community/01796c58772f66a801219c77e4fc04.png@1280w_1l_2o_100sh.png","https://www.baidu.com/"));
        topEntities.add(new HomeBannerEntity("http://img.zcool.cn/community/0154805791ffeb0000012e7edba495.jpg@900w_1l_2o_100sh.jpg","https://www.baidu.com/"));
    }

    @Override
    public void oncreate() {
        super.oncreate();
        createBanner();
        createMenu();
        createTextBanner();
        createHotGoods();
        createVp();

        //请求商品数据
        HashMap<String,Object> map = new HashMap<>();
        map.put("categoryId","14");
        map.put("pageNo",1);
        m.requestGoods(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);

    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if(baseEntity instanceof HomeGoodsEntity){
            HomeGoodsEntity homeGoodsEntity = (HomeGoodsEntity) baseEntity;
            adapter.setNewInstance(homeGoodsEntity.getData());

            //点击跳转到商品详情页
            adapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                    HashMap<String, Object> map = new HashMap<>();
                    Bundle bundle = new Bundle();
                    bundle.putInt("shopId",((HomeGoodsEntity) baseEntity).getData().get(position).getId());
                    map.put(UIField.PAGEBUNDLE,bundle);
                    map.put(UIField.PAGECLAZZ,CommodityActivity.class);
                    startPage(map);
                }
            });
        }
    }
}
